package 泛型的上界.复杂示例;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-25
 * Time: 14:16
 */
class Person implements Comparable<Person> {
    @Override
    public int compareTo(Person o) {
        return 0;
    }
}
class Alg3 {
    public static <E extends Comparable<E>> E findMax(E[] array) {
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
//写一个泛型类，当中的方法，可以求数组中的最大值
class Alg2 {
    public <E extends Comparable<E>> E findMax(E[] array) {
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
//写一个泛型类，当中的方法，可以求数组中的最大值
class Alg<E extends Comparable<E>> {
    public E findMax(E[] array) {
        E max = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
public class TestGeneric {
    public static void main(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, };
        Integer ret = Alg3.findMax(array);
        //Integer ret = alg2.<Integer>findMax(array);
        System.out.println(ret);
    }
    public static void main2(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, };
        Alg2 alg2 = new Alg2();
        Integer ret = alg2.findMax(array);
        //Integer ret = alg2.<Integer>findMax(array);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        Integer[] array = {1, 2, 3, 4, 5, };
        Alg<Integer> alg = new Alg<>();
        Integer ret = alg.findMax(array);
        System.out.println(ret);
    }
}
